package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.service.AccountService;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.service.IVerifyCodeService;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @author: Andy
 * @date: 2020.12.4 18:23
 * @Description: 个人中心控制器
 * @since JDK 1.8
 */
@Controller
public class PersonalController {
    private AccountService accountService;
    private UserinfoService userinfoService;
    private ILogininfoService logininfoService;
    @Autowired
    public void setLogininfoService(ILogininfoService logininfoService) {
        this.logininfoService = logininfoService;
    }
    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
    @Autowired
    public void setUserinfoService(UserinfoService userinfoService) {
        this.userinfoService = userinfoService;
    }

    @RequireLogin       // 登录注解标志
    @RequestMapping("/personal")
    public String personalCenter(Model model){
        // 获取用户登录的账户信息，其中包含了ID
        Logininfo current = UserContext.getCurrent();
        // 通过ID获取用户账户信息 和 用户基本信息
        Account account = accountService.getById(current.getId());
        model.addAttribute("account",account);
        Userinfo userinfo = userinfoService.getById(current.getId());
        model.addAttribute("userinfo",userinfo);
        return "personal";
    }

    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("/main")
    public String mainCenter(){
        return "main";
    }

    /**
     * 进行手机的绑定
     * @phoneNumber:手机号
     * @verifyCode：验证码
     */
    @RequireLogin
    @ResponseBody
    @RequestMapping("/bindPhone")
    public JSONResult bindPhone(String phoneNumber,String verifyCode){
        JSONResult jsonResult = new JSONResult();
        // 因为可能出现异常，这里进行异常处理
        try {
            userinfoService.bindPhone(phoneNumber,verifyCode);
        }catch (Exception ex){
            ex.printStackTrace();
            jsonResult.setMsg("验证失败...");
            jsonResult.setSuccess(false);
        }
        return jsonResult;
    }

    /**
     * 验证邮件
     */
    @RequestMapping("bindEmail")
    public String bingEmail(String uuid, Model model) {
        try {
            this.userinfoService.bindEmail(uuid);
            model.addAttribute("success", true);
        } catch (Exception e) {
            model.addAttribute("success", false);
            model.addAttribute("msg", e.getMessage());
        }
        return "checkmail_result";
    }
}
